package com.ygo.feihua;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cc.chioceFileListView.ChioceFileView;
import com.cc.chioceFileListView.OnChioceFileListener;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;

/**
 * 选择zip文件界面
 * **/
public class NewWjxz extends AppCompatActivity
{
	//是否是选择路径
	private boolean ispath=false;
	private Gongju gj;
	/*
	*0为路径选择
	*1为zip
	*2为图片
	*3为卡表
	*/
	int lx=0;

	private ChioceFileView chioceFileView;
	
	public static final int TYPE_ZIP=0;
	public static final int TYPE_PIC=1;
	public static final int TYPE_LIFLIST=2;
	public static final int TYPE_SINGLE_CUBDANG=3;
	public static final int TYPE_CDB=4;
	public static final int TYPE_PATH=5;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		 
		gj=Gongju.getdx(this);
		
		Intent intent=getIntent();
		lx=intent.getIntExtra("lx",-1);
		String path1=intent.getStringExtra("path");
		
		chioceFileView = new ChioceFileView(this, null);
		Toolbar tool=chioceFileView.getTool();
		
		switch(lx){
			case TYPE_PATH:
				ispath=true;
				tool.setTitle("路径选择");
				break;
			case TYPE_ZIP:
				tool.setTitle("zip选择");
				break;
			case TYPE_PIC:
				tool.setTitle("图片选择");
				break;
			case TYPE_LIFLIST:
				tool.setTitle("卡表选择");
				break;
			case TYPE_SINGLE_CUBDANG:
				tool.setTitle("残局存档选择");
				break;
			case TYPE_CDB:
				tool.setTitle("数据库选择");
				break;
		}
		
		chioceFileView.setDefaultPath(path1);
		
		setSupportActionBar(tool);
		tool.setNavigationIcon(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
		tool.setNavigationOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					Intent intent=new Intent();
					intent.putExtra("path","");
					setResult(-1,intent);
					finish();
					// TODO: Implement this method
				}
			});
		tool.setOnMenuItemClickListener(onMenuItemClick);
		
		chioceFileView.setOnChioceFileListener(new OnChioceFileListener() {
			@Override
			public void onChioceFile(int id, File path) {
				// TODO Auto-generated method stub
				
				String paths = path.toString();
				switch(lx){
					case -1:
						gj.show("请在想选的目录下点确定按钮");
						break;
					case 0:
						if(paths.endsWith(".zip")){
							Intent intent=new Intent();
							intent.putExtra("path", path.toString());
							setResult(TYPE_ZIP,intent);
							finish();
						}else{
							gj.show("请选择zip后缀的文件");
						}
						break;
					case 1:
						if(paths.endsWith(".png") || paths.endsWith(".jpg")||paths.endsWith(".bmp")){
							Intent intent=new Intent();
							intent.putExtra("path",paths);
							setResult(TYPE_PIC,intent);

							finish();
						}else{
							gj.show("请选择png/jpg/bmp后缀的文件");
						}
						break;
					case 2:
						if(path.toString().endsWith(".conf")){
							Intent intent=new Intent();
							intent.putExtra("path",path.toString());
							setResult(TYPE_LIFLIST,intent);

							finish();
						}else{
							gj.show("请选择conf后缀的文件");
						}
						break;
					case 3:
						if(path.toString().endsWith(".cjcd")){
							Intent intent=new Intent();
							intent.putExtra("path",path.toString());
							setResult(TYPE_SINGLE_CUBDANG,intent);

							finish();
						}else{
							gj.show("请选择cjcd后缀的文件");
						}
						break;
					case 4:
						if(path.toString().endsWith(".cdb")){
							Intent intent=new Intent();
							intent.putExtra("path",path.toString());
							setResult(TYPE_CDB,intent);

							finish();
						}else{
							gj.show("请选择cdb后缀的文件");
						}
						break;
				}
				
				
			}
		});
		
		setContentView(chioceFileView);
		
	
	}
	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem p1)
		{
			switch(p1.getItemId()){
					case R.id.action_qd:
					Intent intent=new Intent();
					intent.putExtra("path",chioceFileView.getfile()+"/");
					setResult(TYPE_PATH,intent);

					finish();
						break;
			}


			// TODO: Implement this method
			return true;
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		if(ispath){
		getMenuInflater().inflate(R.menu.menu_qd, menu);
		}
		// TODO: Implement this method
		return super.onCreateOptionsMenu(menu);
	}

	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// TODO: Implement this method
		
		if(keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent=new Intent();
			intent.putExtra("path","");
			setResult(-1,intent);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
		
		
	
	
	
	
	
}
